package app.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.io.IOException;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import app.Main;
import app.conn.GreenhouseConnector;

/**
 * Frame with remote greenhouses list
 * 
 * @author Lukas Vavra
 *
 */
public class RemoteFrame extends JFrame {
	
	private static RemoteFrame instance;
	
	private static Map<Integer, String> greenhouseList; 
	
	private RemoteFrame() {
		add(new JScrollPane(new ListPanel()));
		
		pack();
		setSize(new Dimension(300, 300));
		setLocationRelativeTo(MainFrame.getInstance());
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setVisible(true);
	}
	
	private static void updateList() {
		try {
			greenhouseList = GreenhouseConnector.getUnitList();
			if(greenhouseList != null) {
				
			}
		} catch (IOException e) {
			Main.showError("Chyba připojení", "Nelze se připojit k vzdálené databázi");
			return;
		}
	}
	
	public static RemoteFrame getInstance() {
		if(instance == null) {
			instance = new RemoteFrame();
		}
		return instance;
	}
	
	public static void visible() {
		updateList();
		getInstance().revalidate();
		getInstance().repaint();
		getInstance().setVisible(true);
	}
	
	public static void invisible() {
		getInstance().setVisible(false);
	}
	
	
	private class ListPanel extends JPanel {
		public ListPanel() {
			super(new GridBagLayout());
			GridBagConstraints cs = new GridBagConstraints();
			
			cs.insets = new Insets(10, 20, 10, 20);
			cs.fill = GridBagConstraints.HORIZONTAL;
			cs.gridx = 0;
			cs.weightx = 1;
			
			for(int i = 0; i < 10; i++) {
				cs.gridy = i;
				add(new JButton("<html>shgshgds<br>dklsjgklghks</html>"), cs);
			}
		}
	}
}
